var addrbill = require('../../sys/sys_addr.js')
// 废弃
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    navCur:{
      type:String,
     value: "0"
    },
    themcolor:{
      type:Object,
      default:{}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    op: "",
    delivery_config:{openloacion:-1},
    addrmodel: {}
  },


  ready(){
        var app=getApp();
     var delivery_config=   app.globalData.configdata["delivery_config"] 
     this.setData({delivery_config})    
     
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showop(op, addmodel) {
      var updatemodel = {}
      updatemodel["op"] = op;
      if (!addmodel) {
        addmodel = this.clearmodel(true)
      }
      updatemodel["addrmodel"] = addmodel
      this.setData(updatemodel)
    },
    hideform() {
      this.setData({ op: '' })
    
    },
    changeisdefault() {
      var upatemodel = {};
      upatemodel["addrmodel.isdefault"] = -this.data.addrmodel.isdefault
      this.setData(upatemodel)
    },
    chooseLocation(ev){
      console.log("ev")
      console.log(ev)

      var mydata =ev;
      var that =this;
      addrbill.wxaddrtosysaddr({cityName:mydata.city,countyName:mydata.district,provinceName:mydata.province}).then(function(myaddr){
        console.log("myaddr")
        console.log(myaddr)
        var addrmodel= that.data.addrmodel
        addrmodel["province"] = mydata.province;
        addrmodel["city"] = mydata.city;    
        addrmodel["county"] = mydata.district;
   
  
        if (myaddr) {                    
          var ilen = myaddr.length
          if (myaddr.length < 3) {
            for (; ilen < 3; ilen++) {
              myaddr.push({ id: "", name: "" })
            }
          }
          addrmodel["region"] = myaddr;
          addrmodel["provincecode"] = myaddr[0]["id"];
          addrmodel["citycode"] = myaddr[1]["id"];
          addrmodel["countycode"] = myaddr[2]["id"];
        }
        addrmodel.lat= mydata["latitude"];
        addrmodel.lng= mydata["longitude"];
        addrmodel.addrname= mydata["name"];
        addrmodel.addrinfo= mydata["address"];

        console.log("addrmodel")
        console.log(addrmodel)

        that.setData({addrmodel})
      })

    },
    chosepostion:function(e){


      var delivery_config = this.data.delivery_config

     

      if(delivery_config.loaciontype==1){

       var qqstylemap= this.selectComponent("#qqstylemap")
       var mode=1;
       if(this.data.navCur==3){

        //域名限制
        mode=2
       }
      
      var  addrmodel =this.data.addrmodel
       qqstylemap.showmap(mode,addrmodel,delivery_config.intracity)
       

        return ;
      }

      if(delivery_config.loaciontype==2){
        var app=getApp();
        try{
          var mapqq= app.globalData.configdata.map.qq     
          var url = 'plugin://chooseLocation/index?key=' + mapqq.key + '&referer=' +  mapqq.keyname 
          if(this.data.addrmodel.lat && this.data.addrmodel.lat!=0 ){        
            const location = JSON.stringify({
              latitude: this.data.addrmodel.lat,
              longitude: this.data.addrmodel.lng
            });
            url+= '&location=' + location
          }      
          wx.navigateTo({
          url
          });
  
        }
        catch(ex){
  
          wx.showToast({
            title: '请检测腾讯地图配制是否正确',
          })
        }
      }
      return
      
     
    },
    chooseaddress: function (e) {
      var data = e.detail;
   
      this.setData({
        "addrmodel.region": data
      })
    },
    //选择地区
    pickerblock: function () {
      this.selectComponent("#picker").setinitdata();
    },
    //地址提交
    addrfromsubit(e) {
      var addrmodel = this.data.addrmodel;
      var fromdata = e.detail.value;

      var that = this;
      addrmodel["addrinfo"] = fromdata.addrinfo;
      addrmodel["name"] = fromdata.name;
      addrmodel["addrmoreinfo"] = fromdata.addrmoreinfo;
      addrmodel["isdefault"] = fromdata.isdefault ? 1 : -1;   
      if(this.data.navCur==3){

       if(addrmodel.lat==0|| !addrmodel.lat)
       {
         wx.showToast({
           title: '请选择地址',
         })

        return
       } 
      }  

      var bvalue = false;
      var vdata = {
        "name": ["", "收货人姓名不能为空"],
        "phone": ["", "手机不能为空"],
        "region": ["", "请选择地址"],
        "addrinfo": ["", "详细地址不能为空"]
      }
      var errors = [];
      for (var a in vdata) {
        if (!addrmodel[a] || addrmodel[a] == vdata[a][0]) {
          errors.push(vdata[a][1])
        }
      }

      if (errors.length > 0) {
        wx.showModal({
          title: '错误',
          content: errors.join("\r\n"),
        })
        return;
      }
      else {
        addrmodel.province = addrmodel.region[0].name;
        addrmodel.provincecode = addrmodel.region[0].id;
        addrmodel.city = addrmodel.region[1].name;
        addrmodel.citycode = addrmodel.region[1].id;
        addrmodel.county = addrmodel.region[2].name;
        addrmodel.countycode = addrmodel.region[2].id;
        if ((!addrmodel.provincecode) || (!addrmodel.countycode) || (!addrmodel.citycode)) {
          wx.showModal({
            title: '错误',
            content: "请选择地区",
          })
          return;
        }
        addrbill.addormodify(addrmodel).then(function (ret) {
      

          if (ret["code"] == "success") {
            if (addrmodel.id == 0 ||!addrmodel.id ) {
              addrmodel.id = ret["id"]
            }
            that.triggerEvent("addfun", addrmodel)
            wx.showToast({
              title: '操作成功',
            })
            that.setData({op:""})
            return ;
          }
          wx.showToast({
            title: '操作失败',
          })
        }
        )
      }
    },
    //获取手机号
    getPhoneNumber(e) {
      var that = this;
      var dataset = e.currentTarget.dataset;
      var encryptedData = e.detail.encryptedData;
      var iv = e.detail.iv;
      var code = e.detail.code;

      addrbill.getphone(encryptedData, iv,code).then(function (data) {
        var upatemodel = {};
        if (data["code"] == "success") {
          upatemodel["addrmodel.phone"] = data["obj"].phoneNumber;
        }
        that.setData(upatemodel);

      });

    },
    selectedaddr(ev){
      var detail= ev.detail   
      var that =this;
      addrbill.wxaddrtosysaddr({cityName:detail.ad_info.city,countyName:detail.ad_info.district,provinceName:detail.ad_info.province}).then(function(myaddr){
   
        var addrmodel= that.data.addrmodel
       addrmodel["province"] = detail.ad_info.province;
       addrmodel["city"] = detail.ad_info.city;    
       addrmodel["county"] = detail.ad_info.district;
       addrmodel["lat"]  =detail.location["lat"]
       addrmodel["lng"]  =detail.location["lng"]
       addrmodel["addrinfo"]  =detail.address
       addrmodel["addrname"]  =detail.title

       if (myaddr) {                    
        var ilen = myaddr.length
        if (myaddr.length < 3) {
          for (; ilen < 3; ilen++) {
            myaddr.push({ id: "", name: "" })
          }
        }
        addrmodel["region"] = myaddr;
        addrmodel["provincecode"] = myaddr[0]["id"];
        addrmodel["citycode"] = myaddr[1]["id"];
        addrmodel["countycode"] = myaddr[2]["id"];
      }


       that.setData({addrmodel})
      })


   

    },
    //清空数据
    clearmodel(black) {
      var clearupdate = {
        addrmodel: {
          isdefault: "-1",
          addrinfo: "",
          region: [],
          phone: "",
          name: ""
        }
      }
      if (black) {
        return clearupdate.addrmodel
      }
      else {
        this.setData(clearupdate)
      }
    }
  }
})
